import { createStore } from "redux"
const init = {
    listArr: [
        {
            id: 1, name: "零食", price: 15, num: 0
        },
        {
            id: 2, name: "饮料", price: 5, num: 0
        },
    ],
    shopArr: []
}
function reducer(state = init, action) {
    if (action.type == "NUM") {
        let index = state.listArr.findIndex(i => i.id == action.id)
        action.lei ? (
            state.listArr[index].num++
        ) : (
            state.listArr[index].num > 0 ? state.listArr[index].num-- : 0
        )
        state.shopArr = state.listArr.filter(i => i.num > 0)
        return { ...state }
    }
    return state
}
export default createStore(reducer)